<?php declare(strict_types=1);

namespace App\Util;


class RequestUtil
{

    public static function curlPost(string $url, array $params, array $headers = ["Content-Type: application/x-www-form-urlencoded"])
    {
        try {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            $params = http_build_query($params);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

            //请求类型
            $this_header = ["charset=UTF-8"];
            foreach ($headers as $header) {
                array_push($this_header, $header);
            }
            if (!empty($this_header)) {
                curl_setopt($ch, CURLOPT_HTTPHEADER, $this_header);
            }
            $result = curl_exec($ch);
            if (curl_errno($ch)) {
                throw new \Exception(curl_error($ch));
            }
            curl_close($ch);
            return $result;
        } catch (\Exception $ex) {
            throw new \Exception($ex->getMessage());
        }
    }
}